//данная программа генерирует файлы общих библиотек

#include <iostream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

#include "binfile.h"

#define N 5
#define compCnt 10

BinFile writer, reader[compCnt];
bool rstate[compCnt];
Int32 funcs[compCnt];
inline Int8 min() {
  Int8 r = 255;
  for(size_t i = 0; i < compCnt; i++) {
    if (rstate[i] && (r == 255 || funcs[i] < funcs[r])) r = i;    
  }
  return r;
}

int main () {
  ostringstream oss;

  for(size_t i = 0; i < compCnt; i++) {
    oss << "binfile" << (int)N << "vf" << (int)i << "r.mbf";
    reader[i].open(oss.str(), BIN_FILE_READ);
    oss.str("");    
    reader[i].read(funcs[i]);
    rstate[i] = !reader[i].isEnd();
  }
  
  oss << "binfile" << (int)N << "vf.mbf";
  writer.open(oss.str(), BIN_FILE_WRITE); 
  
  Int8 i;
  while((i = min()) != 255) {
    writer.write(i);
    reader[i].read(funcs[i]);
    rstate[i] = !reader[i].isEnd();        
  }

  return 0;
}

